﻿using SRB.Tg.DrawingStandard;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public class RebarSample
    {
        public static void CreateMainSample(Database db, ref Extents2d ext, Point2d pt, out Line line1, out Line line2, double wP = 710, double wPi = 298, double wM = 195, bool isUpper = true, double gradeLaye = 28, double w0 = 190, string layer = "H钢筋", bool isDim = false, double scale = 1, double R = 100)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                if (isUpper)
                {
                    //Arc arcCal = new Arc(pt1.C3D(), pile_diameter * 0.5, GeTools.DegreeToRadian(0), GeTools.DegreeToRadian(360));
                    PLPloter.AddLine(db,pt.C2D(), pt.C2D(wP - 5), ref ext, layer);
                    line1 = new Line(pt.C3D(wP - 5), pt.C3D(wP + w0 + 5));
                    double ang0 = GeTools.RadianToDegree(Math.Atan(w0 / gradeLaye));
                    Arc arc1 = new Arc(pt.C3D(wP - 5, R), R, GeTools.DegreeToRadian(270), GeTools.DegreeToRadian(-ang0)) { LayerId = lt[layer] };
                    recorder.AppendEntity((Entity)arc1);
                    tr.AddNewlyCreatedDBObject(arc1, true);

                    Arc arc2 = new Arc(pt.C3D(wP + w0 + 5, -R + gradeLaye), R, GeTools.DegreeToRadian(90), GeTools.DegreeToRadian(180 - ang0)) { LayerId = lt[layer] };
                    recorder.AppendEntity((Entity)arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);



                    PLPloter.AddLine(db,arc2.EndPoint.C2D(), arc1.EndPoint.C2D(), ref ext, layer);
                    line2 = new Line(arc1.EndPoint.C3D(), arc2.EndPoint.C3D());
                    PLPloter.AddLine(db,arc2.StartPoint.C2D(), pt.C2D(wP + w0 + wPi, gradeLaye), ref ext, layer);
                }
                else
                {
                    PLPloter.AddLine(db,pt.C2D(), pt.C2D(wP - 5), ref ext, layer);
                    line1 = new Line(pt.C3D(wP - 5), pt.C3D(wP + w0 + 5));
                    double ang0 = GeTools.RadianToDegree(Math.Atan(w0 / gradeLaye));
                    Arc arc1 = new Arc(pt.C3D(wP - 5, -R), R, GeTools.DegreeToRadian(ang0), GeTools.DegreeToRadian(90)) { LayerId = lt[layer] };

                    recorder.AppendEntity((Entity)arc1);
                    tr.AddNewlyCreatedDBObject(arc1, true);

                    Arc arc2 = new Arc(pt.C3D(wP + w0 + 5, R - gradeLaye), R, GeTools.DegreeToRadian(180 + ang0), GeTools.DegreeToRadian(-90)) { LayerId = lt[layer] };

                    recorder.AppendEntity((Entity)arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);
                    //Arc arc3 = new Arc(pt.C3D(w * 0.5 - 1.4 + w0, -1 * scale + gradeLaye), 1 * scale, GeTools.DegreeToRadian(90), GeTools.DegreeToRadian(180 - ang0)) { ColorIndex = 4 };



                    PLPloter.AddLine(db,arc2.StartPoint.C2D(), arc1.StartPoint.C2D(), ref ext, layer);
                    line2 = new Line(arc1.StartPoint.C3D(), arc2.StartPoint.C3D());
                    PLPloter.AddLine(db,arc2.EndPoint.C2D(), pt.C2D(wP + w0 + wPi, -gradeLaye), ref ext, layer);
                }

                tr.Commit();
            }



        }


        public static void AddStandardLineSample(Point2d point, ref Extents2d ext, string layer, eDB N,double ang,  string txtmid = "", string txtSide = "", double lenline = 70, double len = 5, double scale = 1, double textH = 2,bool isDim=false)
        {

            Database db = HostApplicationServices.WorkingDatabase;
            Point2d stPoint = point.C2D(-lenline * 0.5), endPoint = point.C2D(-lenline * 0.5);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                string D = ((int)N).ToString();
                double dia = double.Parse(D);
                double value = dia;
                if (dia > 16)
                    value = dia * 1.4;
                else
                    value = dia * 1.25;

                stPoint = point.C2D(-lenline * 0.5*Math.Cos(ang), -lenline * 0.5*Math.Sin(ang));
                endPoint = point.C2D(lenline * 0.5 * Math.Cos(ang), lenline * 0.5 * Math.Sin(ang));


                Line line = new Line(stPoint.C3D(), endPoint.C3D());
                line.LayerId = lt[layer];
                line.LinetypeScale = scale;
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                if (len > 0)
                {
                    Point2d p1 = stPoint.C2D();
                    Point2d p2 = stPoint.C2D(-len * 0.5 * Math.Sin(ang), len * 0.5 * Math.Cos(ang));
                    line = new Line(p1.C3D(), p2.C3D());
                    line.LayerId = lt[layer];
                    line.LinetypeScale = scale;
                    modelSpace.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));

                    p1 = endPoint.C2D();
                    p2 = endPoint.C2D(-len * 0.5 * Math.Sin(ang), len * 0.5 * Math.Cos(ang));
                    line = new Line(p1.C3D(), p2.C3D());
                    line.LayerId = lt[layer];
                    line.LinetypeScale = scale;
                    modelSpace.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                }
                if (isDim)
                {
                    if(ang!=0)
                        TextPloter.AddDBText(db,point.C3D(-3 * scale * Math.Cos(ang), -3 * scale * Math.Sin(ang)), ref ext, txtmid, scale, textH, "SRB.HT", ang, TextHorizontalMode.TextCenter);
                    else
                        TextPloter.AddDBText(db,point.C3D(-0 * scale * Math.Cos(ang), -3 * scale * Math.Sin(ang)), ref ext, txtmid, scale, textH, "SRB.HT", ang, TextHorizontalMode.TextCenter);
                    if (len > 0)
                    {
                        TextPloter.AddDBText(db,stPoint.C2D(-0.2 * scale).C3D(0.5 * scale * Math.Cos(ang), 0), ref ext, txtSide, scale, textH, "SRB.HT", GeTools.DegreeToRadian(90) + ang, TextHorizontalMode.TextLeft);
                        TextPloter.AddDBText(db,endPoint.C2D(0.2 * scale).C3D(2.5 * scale * Math.Cos(ang), 2.5 * scale * Math.Sin(ang)), ref ext, txtSide, scale, textH, "SRB.HT", GeTools.DegreeToRadian(90) + ang, TextHorizontalMode.TextLeft);
                    }
                }

                tr.Commit();
            }
        }

        public static void AddStandardLineSampleNew(Point2d point, ref Extents2d ext, string layer, eDB N, double ang, string txtmid = "",  double lenline = 70, double len = 5, double scale = 1, double textH = 2, bool isDim = false)
        {

            Database db = HostApplicationServices.WorkingDatabase;
            Point2d stPoint = point.C2D(-lenline * 0.5), endPoint = point.C2D(-lenline * 0.5);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                string D = ((int)N).ToString();
                double dia = double.Parse(D);
                double value = dia;
                if (dia > 16)
                    value = dia * 1.4;
                else
                    value = dia * 1.25;

                stPoint = point.C2D(-lenline * 0.5 * Math.Cos(ang), -lenline * 0.5 * Math.Sin(ang));
                endPoint = point.C2D(lenline * 0.5 * Math.Cos(ang), lenline * 0.5 * Math.Sin(ang));


                Line line = new Line(stPoint.C3D(), endPoint.C3D());
                line.LayerId = lt[layer];
                line.LinetypeScale = scale;
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                if (len > 0)
                {
                    Arc arc2 = new Arc(stPoint.C3D(0, 5), 5, GeTools.DegreeToRadian(135), GeTools.DegreeToRadian(270)) { LayerId = lt[layer] };
                    modelSpace.AppendEntity((Entity)arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);
                    Point2d p1 = stPoint.C2D(-5*Math.Sin(GeTools.DegreeToRadian(135)),5+ 5 * Math.Sin(GeTools.DegreeToRadian(45)));
             
                    Point2d p2 = stPoint.C2D(-5 * Math.Sin(GeTools.DegreeToRadian(135)), 5 + 5 * Math.Sin(GeTools.DegreeToRadian(45))).C2D(-len * 0.5 * Math.Sin(ang-GeTools.DegreeToRadian(45)), len * 0.5 * Math.Cos(ang - GeTools.DegreeToRadian(45)));
                    line = new Line(p1.C3D(), p2.C3D());
                    line.LayerId = lt[layer];
                    line.LinetypeScale = scale;
                    modelSpace.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));

                    arc2 = new Arc(endPoint.C3D(0, 5), 5, GeTools.DegreeToRadian(270), GeTools.DegreeToRadian(360)) { LayerId = lt[layer] };
                    modelSpace.AppendEntity((Entity)arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);
                    p1 = endPoint.C2D(5,5);
                    p2 = endPoint.C2D(5, 5).C2D(-len * 0.5 * Math.Sin(ang), len * 0.5 * Math.Cos(ang));
                    line = new Line(p1.C3D(), p2.C3D());
                    line.LayerId = lt[layer];
                    line.LinetypeScale = scale;
                    modelSpace.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                }
                if (isDim)
                {
                    if (ang != 0)
                        TextPloter.AddDBText(db,point.C3D(-3 * scale * Math.Cos(ang), -3 * scale * Math.Sin(ang)), ref ext, txtmid, scale, textH, "SRB.HT", ang, TextHorizontalMode.TextCenter);
                    else
                        TextPloter.AddDBText(db,point.C3D(-0 * scale * Math.Cos(ang), -3 * scale * Math.Sin(ang)), ref ext, txtmid, scale, textH, "SRB.HT", ang, TextHorizontalMode.TextCenter);                   
                }

                tr.Commit();
            }
        }

        public static void AddStandardOneSideLineSampleNew(Point2d point, ref Extents2d ext, string layer, eDB N, double ang, string txtmid = "", double lenline = 70, double len = 5, double scale = 1, double textH = 2, bool isDim = false)
        {

            Database db = HostApplicationServices.WorkingDatabase;
            Point2d stPoint = point.C2D(-lenline * 0.5), endPoint = point.C2D(-lenline * 0.5);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                string D = ((int)N).ToString();
                double dia = double.Parse(D);
                double value = dia;
                if (dia > 16)
                    value = dia * 1.4;
                else
                    value = dia * 1.25;

                stPoint = point.C2D(-lenline * 0.5 * Math.Cos(ang), -lenline * 0.5 * Math.Sin(ang));
                endPoint = point.C2D(lenline * 0.5 * Math.Cos(ang), lenline * 0.5 * Math.Sin(ang));


                Line line = new Line(stPoint.C3D(), endPoint.C3D());
                line.LayerId = lt[layer];
                line.LinetypeScale = scale;
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                if (len > 0)
                {
                    //Arc arc2 = new Arc(stPoint.C3D(0, 5), 5, GeTools.DegreeToRadian(135), GeTools.DegreeToRadian(270)) { LayerId = lt[layer] };
                    //modelSpace.AppendEntity((Entity)arc2);
                    //tr.AddNewlyCreatedDBObject(arc2, true);
                    //Point2d p1 = stPoint.C2D(-5 * Math.Sin(GeTools.DegreeToRadian(135)), 5 + 5 * Math.Sin(GeTools.DegreeToRadian(45)));

                    //Point2d p2 = stPoint.C2D(-5 * Math.Sin(GeTools.DegreeToRadian(135)), 5 + 5 * Math.Sin(GeTools.DegreeToRadian(45))).C2D(-len * 0.5 * Math.Sin(ang - GeTools.DegreeToRadian(45)), len * 0.5 * Math.Cos(ang - GeTools.DegreeToRadian(45)));
                    //line = new Line(p1.C3D(), p2.C3D());
                    //line.LayerId = lt[layer];
                    //line.LinetypeScale = scale;
                    //modelSpace.AppendEntity(line);
                    //tr.AddNewlyCreatedDBObject(line, true);
                    //ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));

                    Arc arc2 = new Arc(endPoint.C3D(0, 5), 5, GeTools.DegreeToRadian(270), GeTools.DegreeToRadian(360)) { LayerId = lt[layer] };
                    modelSpace.AppendEntity((Entity)arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);
                    Point2d p1 = endPoint.C2D(5, 5);
                    Point2d p2 = endPoint.C2D(5, 5).C2D(-len * 0.5 * Math.Sin(ang), len * 0.5 * Math.Cos(ang));
                    line = new Line(p1.C3D(), p2.C3D());
                    line.LayerId = lt[layer];
                    line.LinetypeScale = scale;
                    modelSpace.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                }
                if (isDim)
                {
                    if (ang != 0)
                        TextPloter.AddDBText(db,point.C3D(-3 * scale * Math.Cos(ang), -3 * scale * Math.Sin(ang)), ref ext, txtmid, scale, textH, "SRB.HT", ang, TextHorizontalMode.TextCenter);
                    else
                        TextPloter.AddDBText(db,point.C3D(-0 * scale * Math.Cos(ang), -3 * scale * Math.Sin(ang)), ref ext, txtmid, scale, textH, "SRB.HT", ang, TextHorizontalMode.TextCenter);
                }

                tr.Commit();
            }
        }

        public static void AddLineSampleNew(Point2d point, ref Extents2d ext, string layer, eDB N, double ang, string txtmid = "", double lenline = 70,  double scale = 1, double textH = 2, bool isDim = false)
        {

            Database db = HostApplicationServices.WorkingDatabase;
            Point2d stPoint = point.C2D(-lenline * 0.5), endPoint = point.C2D(-lenline * 0.5);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                string D = ((int)N).ToString();
                double dia = double.Parse(D);
                double value = dia;
                if (dia > 16)
                    value = dia * 1.4;
                else
                    value = dia * 1.25;

                stPoint = point.C2D(-lenline * 0.5 * Math.Cos(ang), -lenline * 0.5 * Math.Sin(ang));
                endPoint = point.C2D(lenline * 0.5 * Math.Cos(ang), lenline * 0.5 * Math.Sin(ang));


                Line line = new Line(stPoint.C3D(), endPoint.C3D());
                line.LayerId = lt[layer];
                line.LinetypeScale = scale;
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
              
                if (isDim)
                {
                    if (ang != 0)
                        TextPloter.AddDBText(db,point.C3D(-3 * scale * Math.Cos(ang), -3 * scale * Math.Sin(ang)), ref ext, txtmid, scale, textH, "SRB.HT", ang, TextHorizontalMode.TextCenter);
                    else
                        TextPloter.AddDBText(db,point.C3D(-0 * scale * Math.Cos(ang), -3 * scale * Math.Sin(ang)), ref ext, txtmid, scale, textH, "SRB.HT", ang, TextHorizontalMode.TextCenter);
                }

                tr.Commit();
            }
        }

    }
}
